#ifndef MODEL_DATA_H
#define MODEL_DATA_H

#include <QString>
#include <QIcon>
#include <QList>
#include <QDateTime>

#include "base.qpb.h"

namespace util {

static inline QString formatTime(int64_t timestamp) {
    QDateTime dateTime = QDateTime::fromSecsSinceEpoch(timestamp);
    return dateTime.toString("MM-dd HH:mm:ss");
}

static inline QPixmap makePixmap(const QByteArray& byteArray) {
    QPixmap pixmap;
    pixmap.loadFromData(byteArray);
    return pixmap;
}

} // namespace util

namespace model {

class UserInfo {
public:
    QString userId;
    QString nickname;
    QIcon avatar;

    UserInfo() {}

    UserInfo(const QString& uid, const QString& name, const QByteArray& byteArray)
        : userId(uid), nickname(name), avatar(QIcon(util::makePixmap(byteArray)))
    {}

    UserInfo(const quan_mian::UserInfo& user);

    operator quan_mian::UserInfo() const;
};

class InterviewInfo {
public:
    QString interviewId;
    UserInfo sender;
    int64_t timestamp;
    QString title;
    QString content;

    InterviewInfo() {}

    InterviewInfo(const QString& id, const UserInfo& info, int64_t time, const QString& ti, const QString& cont)
        : interviewId(id), sender(info), timestamp(time), title(ti), content(cont)
    {}

    InterviewInfo(const quan_mian::InterviewInfo& interview);

    operator quan_mian::InterviewInfo() const;
};

class CompanyInfo {
public:
    QString companyId;
    QString companyName;
    QIcon avatar;
    int64_t windowCnt;
    int64_t timestamp;
    int64_t ci;
    QList<QString> topTags;
    QList<int64_t> topTagsPct;
    QList<QString> upTags;
    QList<int64_t> upTagsPct;

    CompanyInfo() {}

    CompanyInfo(const quan_mian::CompanyInfo& info);
};

class QuestionInfo{
public:
    QString questionId;
    QString interviewId;
    QString questionClusterId;
    QString content;
    QList<QString> tags;

    QuestionInfo() {}

    QuestionInfo(const quan_mian::QuestionInfo& info);
};

class QuestionClusterInfo {
public:
    QString questionClusterId;
    QList<QuestionInfo> questions;
    int64_t cnt;
    QList<QString> tags;
    QString standardAnswer;

    QuestionClusterInfo() {}

    QuestionClusterInfo(const quan_mian::QuestionClusterInfo& info);
};

class SearchLimits {
public:
    QString searchKey;

    operator quan_mian::SearchLimits() const;
};

inline UserInfo::UserInfo(const quan_mian::UserInfo &user)
    : userId(user.userId())
    , nickname(user.nickname())
    , avatar(QIcon(util::makePixmap(user.avatar())))
{}

inline UserInfo::operator quan_mian::UserInfo() const
{
    quan_mian::UserInfo pbObj;
    pbObj.setUserId(userId);
    pbObj.setNickname(nickname);
    return pbObj;
}

inline InterviewInfo::InterviewInfo(const quan_mian::InterviewInfo &interview)
    : interviewId(interview.interviewId())
    , sender(interview.sender())
    , timestamp(interview.timestamp())
    , title(interview.title())
    , content(interview.content())
{}

inline InterviewInfo::operator quan_mian::InterviewInfo() const
{
    quan_mian::InterviewInfo pbObj;
    pbObj.setInterviewId(interviewId);
    pbObj.setSender(sender);
    pbObj.setTimestamp(timestamp);
    pbObj.setTitle(title);
    pbObj.setContent(content);
    return pbObj;
}

inline CompanyInfo::CompanyInfo(const quan_mian::CompanyInfo &info)
    : companyId(info.companyId())
    , companyName(info.companyName())
    , avatar(QIcon(util::makePixmap(info.avatar())))
    , windowCnt(info.windowCnt())
    , timestamp(info.timestamp())
    , ci(info.ci())
    , topTags(info.topTags())
    , upTags(info.upTags())
{
    for(const auto& e : info.topTagsPct())
        topTagsPct.append(e);

    for(const auto& e : info.upTagsPct())
        upTagsPct.append(e);
}

inline QuestionInfo::QuestionInfo(const quan_mian::QuestionInfo &info)
    : questionId(info.questionId())
    , interviewId(info.interviewId())
    , questionClusterId(info.questionClusterId())
    , content(info.content())
    , tags(info.tags())
{}

inline QuestionClusterInfo::QuestionClusterInfo(const quan_mian::QuestionClusterInfo &info)
    : questionClusterId(info.questionClusterId())
    , cnt(info.cnt())
    , tags(info.tags())
    , standardAnswer(info.standardAnswer())
{
    questions.reserve(info.questions().size());
    for(const auto& e : info.questions())
        questions.append(e);
}

inline SearchLimits::operator quan_mian::SearchLimits() const
{
    quan_mian::SearchLimits pbObj;
    pbObj.setSearchKey(searchKey);
    return pbObj;
}

} // namespace model

#endif // MODEL_DATA_H
